package com.bloveoacom.util;

import com.bloveoacom.base.AppContext;
import com.bloveoacom.base.UserInfoBean;
import com.bloveoacom.http.MessageModel;
import com.bloveoacom.http.RxBus;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import timber.log.Timber;

import static com.bloveoacom.http.MessageModel.SHARE_OK;

public class ShareUtils {

	/**
	 * 微信朋友圈分享 title用不上
	 */
	public static void shareFriends(String path,int type) {
		Platform plat = ShareSDK.getPlatform(type==0? WechatMoments.NAME: Wechat.NAME);
		Platform.ShareParams sp = new Platform.ShareParams();
		sp.setShareType(Platform.SHARE_IMAGE);
		//sp.setImageUrl(SHARE_PIC_URL);
		sp.setImagePath(path);
		plat.setPlatformActionListener(new MyPlatformActionListener()); // 设置分享事件回调
		plat.share(sp);

	}

	static class MyPlatformActionListener implements PlatformActionListener {

		@Override
		public void onError(Platform arg0, int arg1, Throwable arg2) {
			// TODO Auto-generated method stub
			Timber.e("分享错误" + arg1 + "===" + arg2.toString());
			AppContext.mHandler.post(new Runnable() {
				public void run() {
					ToastUtil.makeShortText(AppContext.context(), "分享失败");
				}
			});

		}

		@Override
		public void onComplete(Platform arg0, int arg1,
							   HashMap<String, Object> arg2) {
			AppContext.mHandler.post(new Runnable() {
				public void run() {
					ToastUtil.makeShortText(AppContext.context(), "分享成功");
					//发送分享成功的消息
					if(UserInfoBean.getInstance().isLogin())
						RxBus.getInstance().send(new MessageModel(SHARE_OK));
				}
			});
		}

		@Override
		public void onCancel(Platform arg0, int arg1) {
			AppContext.mHandler.post(new Runnable() {
				public void run() {
					ToastUtil.makeShortText(AppContext.context(), "取消分享");
				}
			});
		}
	}



}


